package com.erpanderp.wam.usm.view.projectandassignment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.projectandassignment.ProjectDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;



@ManagedBean
@ViewScoped
public class CreateProjectBean implements Serializable {

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	String createDate ="";
	private List<Employee> ownerOfProject = new ArrayList<Employee>();
	private Map<String, Long> ownerOfProjectMap = new HashMap<String, Long>();
	

	private boolean FORM_VALID;
	private Project project = new Project();
	private Employee employee = new Employee();
	ProjectDAO projectDAO = new ProjectDAO();
	EmployeeDAO employeeDAO =  new EmployeeDAO();
	
	private WAMUser wamUser;
	private String loginUserName=""; 
	private Long selectedPMId;
	
	
	public CreateProjectBean() {
		 
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		project.setCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
		project.setProjectCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
				
		ownerOfProject = employeeDAO.getCompetencyLeads(); 
		for (Employee projectOwner : ownerOfProject) {
			ownerOfProjectMap.put(projectOwner.getFirstName()+" "+projectOwner.getLastName(), projectOwner.getEmployeeId());
		}
		
	}
	
	
	public void conformationPopup(ActionEvent e){	
		System.out.println("Before validate=======");
		validateForm();
		System.out.println("After validate=======");
		if(FORM_VALID){
			System.out.println("IN conformationPopup");
			RequestContext.getCurrentInstance().execute("confirmation.show();");
		}
	
	}
	
	public void saveProject(ActionEvent e){	
		validateForm();
		if(FORM_VALID){
			project.setCreateDate(new Date());
			project.setStatus("ACTIVE");
			System.out.println("selectedPMId==============="+selectedPMId);
			System.out.println("employeeDAO.getEmployeeByEmployeeId(selectedPMId)==========="+employeeDAO.getEmployeeByEmployeeId(selectedPMId));
			project.setOwner(employeeDAO.getEmployeeByEmployeeId(selectedPMId));
			project.setCreater(wamUser.getEmployee());			
			projectDAO.createProject(project);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, project.getProjectName() + " Project created successfully.", ""));
			project = new Project();
		}
		System.out.println("IN SAVE MODULE");
	}
	

	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(project.getProjectName(), "Project Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		if(ValidationUtil.isEmpty(project.getClient(), "Client Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(project.getClientLocation(), "Client Location", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		if(ValidationUtil.isEmpty(project.getClientAddress(), "Client Address", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(project.getContactPerson(), "Client Contact Person", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(project.getContactNumber(), "Client Contact Number", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(project.getPeriodFrom() == null ? null : project.getPeriodFrom().toString(), "Project Start Date", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(project.getPeriodTo() == null ? null : project.getPeriodTo().toString(), "Project End Date", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		if(project.getPeriodTo().before(project.getPeriodFrom())){
			   FORM_VALID = false;
			   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN," Project end date should be greater than project start date", ""));
		}
		
		if(ValidationUtil.isEmpty(project.getProjectCreatedBy(), "Project Created By", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	/*	
		if (project.getNoOfResourecs()>0)
		{
			FORM_VALID = true;
		
		}
		else
		{
			FORM_VALID = false;

			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, project.getNoOfResourecs() + " No Of resource must be greater than 0", ""));
			System.out.println("Please enter no.of res>0");
			
		}*/
		if (ValidationUtil.isEmpty(project.getNoOfResourecs().toString(), "No. of Resources", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}else{
			int totalResources = 0;
			
			try
			{
				totalResources = project.getNoOfResourecs();
			}
			catch (NumberFormatException nfe)
			{
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_WARN, "Please enter no. of resorces count is numeric .", ""));
				FORM_VALID = false;
				System.out.println("==========INSIDE CATCH BLOCK======NumberFormatException=====");
			}
		}
		System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);
		
	}
	
	
	
	//GENERATE SETTERS AND GETTERS
	
	public String getCreateDate() {
		return createDate;
	}
	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public String getLoginUserName() {
		return loginUserName;
	}

	public void setLoginUserName(String loginUserName) {
		this.loginUserName = loginUserName;
	}

	public Map<String, Long> getOwnerOfProjectMap() {
		return ownerOfProjectMap;
	}

	public void setOwnerOfProjectMap(Map<String, Long> ownerOfProjectMap) {
		this.ownerOfProjectMap = ownerOfProjectMap;
	}

	public Long getSelectedPMId() {
		return selectedPMId;
	}

	public void setSelectedPMId(Long selectedPMId) {
		this.selectedPMId = selectedPMId;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}
